package com.djx.commons.error.error;

import com.djx.commons.response.Result;
import com.djx.commons.response.ResultCode;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

//自定义异常处理器 : 声明异常处理器  对异常的统一处理
@ControllerAdvice //controller增强处理器
public class BaseExceptionHandler {


    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result error(Exception e){
        if(e.getClass() == CommonException.class){
            CommonException ce = (CommonException) e; //抛出异常信息部分
            return new Result(ce.getResultCode());
        }else{
            return new Result(ResultCode.CONTACTANADMINISTRATOR); //抛出具体的值
        }
    }

    @ExceptionHandler(value = AuthorizationException.class)

    @ResponseBody
    public Result error(AuthorizationException e){
//        String message = e.getMessage();

        return new Result(ResultCode.UN_AUTHORISE);
    }

    @ExceptionHandler(value = NullPointerException.class)
    @ResponseBody
    public Result error(NullPointerException e){
        return new Result(ResultCode.PARAMS_IS_NULL);
    }

}
